﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для wEnterpriseInfo.xaml
    /// </summary>
    public partial class wEnterpriseInfo : Window
    {
        private routelistEntities routelistEntities;

        public wEnterpriseInfo()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            if (true == SaveData())
            {
                Close();
            }
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            routelistEntities = new RL.routelistEntities();
            ucSelectPerson1.DBContext = routelistEntities;
            ucSelectPerson2.DBContext = routelistEntities;
            // Загрузить данные в Enterprise. Можно изменить этот код как требуется.
            System.Windows.Data.CollectionViewSource enterpriseViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("enterpriseViewSource")));
            //System.Data.Objects.ObjectQuery<RL.Enterprise> enterpriseQuery = this.GetEnterpriseQuery(routelistEntities);
            var data = (from o in routelistEntities.Enterprise select o).FirstOrDefault();
            if (null == data)
            {
                data = new Enterprise();
                routelistEntities.Enterprise.AddObject(data);
                SaveData();
            }
            //enterpriseViewSource.Source = data; //enterpriseQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
            grid1.DataContext = data;
        }

        private bool IsModified()
        {
            bool bResult = false;
            if (null == routelistEntities) return false;
            bResult = routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted).Count() > 0 || routelistEntities.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Modified).Count() > 0;
            return bResult;
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (IsModified())
            {
                var result = MessageBox.Show("Сохранить изменения?", "Предупреждение", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation, MessageBoxResult.Yes);
                switch (result)
                {
                    case MessageBoxResult.Yes:
                        if (false == SaveData())
                        {
                            e.Cancel = true;
                        }
                        break;
                    case MessageBoxResult.Cancel:
                        e.Cancel = true;
                        break;
                }

            }
        }

        private bool SaveData()
        {
            bool bResult = true;
            try
            {
                routelistEntities.SaveChanges();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), Properties.Resources.DataSavingError, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                bResult = false;
            }
            return bResult;
        }

        private System.Data.Objects.ObjectQuery<Enterprise> GetEnterpriseQuery(routelistEntities routelistEntities)
        {
            // Автоматически созданный код

            System.Data.Objects.ObjectQuery<RL.Enterprise> enterpriseQuery = routelistEntities.Enterprise;
            // Возвращает ObjectQuery.
            return enterpriseQuery;
        }
    }
}
